#include <iostream>
#include <sys/time.h>

namespace Tool {
    enum class DATETIME_FORMAT {CONTINUE, STANDARD};

    // 获取本地时间字符串
    static std::string local_time_str(DATETIME_FORMAT format = DATETIME_FORMAT::STANDARD) {
        timeval current_time {};
        gettimeofday(&current_time, nullptr);

        tm current_tm {};
        localtime_r(&current_time.tv_sec, &current_tm);

        char buf[64] {};
        switch (format) {
            case DATETIME_FORMAT::CONTINUE:
                snprintf(buf, sizeof buf - 1, "%04d%2d%02d%02d%02d%02d%03d", 
                    current_tm.tm_year + 1900, current_tm.tm_mon + 1, current_tm.tm_mday,
                    current_tm.tm_hour, current_tm.tm_min, current_tm.tm_sec, current_time.tv_usec / 1000);
                break;
            case DATETIME_FORMAT::STANDARD:
                snprintf(buf, sizeof buf - 1, "%04d-%2d-%02d %02d:%02d:%02d.%03d", 
                    current_tm.tm_year + 1900, current_tm.tm_mon + 1, current_tm.tm_mday,
                    current_tm.tm_hour, current_tm.tm_min, current_tm.tm_sec, current_time.tv_usec / 1000);
                break;
        }
        return buf;
    }
}
